//
//  YLVisitorView.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/1.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit
import SnapKit

protocol YLVisitorViewDelegate : NSObjectProtocol{
    func loginOperation()
}


//访客视图

class YLVisitorView: UIView {
    //定义要执行的闭包
    var callBack:(() -> ())?
    
    weak var delegate :YLVisitorViewDelegate?
    
    
    //懒加载
    //旋转图
    private lazy var cycleImageV : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //罩层
    private lazy var maskImageV : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    //主页视图
    private lazy var iconImageV :UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    //描述label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .center
        return label
    }()
    
    //两个按钮
    private lazy var regBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
//        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .highlighted)
        
        //点击事件
        btn.addTarget(self, action: #selector(regBtnAction), for: .touchUpInside)
        
        //取消高亮效果
        btn.adjustsImageWhenHighlighted = false
        return btn
    }()
    
    private lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        //        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .highlighted)
        
        //点击事件
        btn.addTarget(self, action: #selector(loginBtnAction), for: .touchUpInside)
        
        //取消高亮效果
        btn.adjustsImageWhenHighlighted = false
        return btn
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置布局
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //添加控件
        addSubview(cycleImageV)
        addSubview(maskImageV)
        addSubview(iconImageV)
        addSubview(messageLabel)
        addSubview(regBtn)
        addSubview(loginBtn)
        
        //约束
        cycleImageV.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageV.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageV)
        }
        
        iconImageV.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageV)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageV.snp.bottom)
            make.centerX.equalTo(cycleImageV)
            make.width.equalTo(224)
        }
        
        regBtn.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.top.equalTo(regBtn)
            make.size.equalTo(regBtn)
        }
       
    }
    
    //开启旋转动画
    private func startAnimation () {
        //创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //目的地
        anim.toValue = 2 * M_PI
        
        //次数
        anim.repeatCount = MAXFLOAT
        
        //时长
        anim.duration = 20
        
        //失去焦点不然停止动画
        anim.isRemovedOnCompletion = false
        
        //执行
        cycleImageV.layer.add(anim, forKey: nil)
        
    }
    
    //修改访客视图信息
    
    func changeVisitorInfo (imageName: String?, message: String?) {
        if let imgName = imageName, let msg = message {
            iconImageV.image = UIImage(named: imgName)
            messageLabel.text = msg
            //隐藏旋转视图
            cycleImageV.isHidden = true
        } else {
            startAnimation()
        }
        
    }
    
    //点击注册和登陆事件
    @objc private func regBtnAction () {
        callBack?()
        
        delegate?.loginOperation()
    }
    
    @objc private func loginBtnAction () {
        callBack?()
        delegate?.loginOperation()
    }

}
/*
 1.设置哪个view
 2.设置这个view的哪个属性
 3.属性相对于控件的方式 (等于 大于 小于 equal)
 4.相对于下个控件
 5.相对于下个控件的哪个属性
 6.倍数
 7.偏移量
 */

//        addConstraint(NSLayoutConstraint(item: cycleImageV, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
